package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.goods.MarketGoods;
import com.barbeque.mall.bean.goods.MarketGoodsExample;
import com.barbeque.mall.bean.keyword.MarketKeyword;
import com.barbeque.mall.bean.user.MarketSearchHistory;
import com.barbeque.mall.bean.user.MarketSearchHistoryExample;
import com.barbeque.mall.bean.wx.search.*;
import com.barbeque.mall.mapper.MarketGoodsMapper;
import com.barbeque.mall.mapper.MarketKeywordMapper;
import com.barbeque.mall.mapper.MarketSearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author why
 * @since 2022/07/20 15:42
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    //默认搜索
    @Override
    public WxMarketKeyword selectDefaultKeyword() {
        WxMarketKeyword wxMarketKeyword = marketKeywordMapper.selectWxByPrimaryKey(6);

        return wxMarketKeyword;
    }

    //热点搜索
    @Override
    public List<WxMarketKeyword> selectHotKeywordList() {
        WxMarketKeywordExample wxMarketKeywordExample = new WxMarketKeywordExample();
        WxMarketKeywordExample.Criteria criteria = wxMarketKeywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<WxMarketKeyword> wxMarketKeywords = marketKeywordMapper.selectWxByExample(wxMarketKeywordExample);
        //marketKeywordMapper.selectWxByExample()
        return wxMarketKeywords;
    }

    //历史搜索
    @Override
    public List<WxMarketSearchHistory> selectHistoryKeyword() {
        WxMarketSearchHistoryExample wxMarketSearchHistoryExample = new WxMarketSearchHistoryExample();
        WxMarketSearchHistoryExample.Criteria criteria = wxMarketSearchHistoryExample.createCriteria();

        List<WxMarketSearchHistory> wxMarketSearchHistories =
                marketSearchHistoryMapper.selectWxByExample(wxMarketSearchHistoryExample);

        return wxMarketSearchHistories;
    }

    @Override
    public List<String> getGoodsName(String keyword) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        ArrayList<String> strings = new ArrayList<>();
        for (MarketGoods marketGood : marketGoods) {
            String name = marketGood.getName();
            strings.add(name);
        }
        List<String> nameList = strings;
        return nameList;
    }

    /**
     * 在搜索后插入keyword
     * @param keyword
     */
    @Override
    public void insertKeyword(String keyword) {
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setKeyword(keyword);
        marketSearchHistoryMapper.insertSelective(marketSearchHistory);
    }




    @Override
    public void clearHistory() {
        marketSearchHistoryMapper.deleteHistory();
    }


}
